#include "gdata.h"
uint8_t u1Buff[256];
uint8_t u3Buff[256];
uint8_t u4Buff[256];
uint8_t u6Buff[256];
uint8_t u1Len = 0;
uint8_t u3Len = 0;
uint8_t u4Len = 0;
uint8_t u6Len = 0;
uint8_t buff[4];
IrState g_ir_state = IR_IDLE;
char esp_send_buff[512];
uint32_t g_time_count = 0;
uint8_t g_cur_send_pulse = 0;
uint8_t    g_capture_flag = 0;
uint8_t    g_send_code_flag = 0;
uint8_t    g_key = 0;
uint8_t    g_out = 0;
ReportType g_report;
RecordType g_flash_record;
FlashHead  g_flash_head;
float   g_temp1,g_temp2,g_humi1,g_humi2;
uint16_t g_pm10,g_pm25;

void Relay_Ctrl(uint8_t num,uint8_t action)
{
	if(action)
		g_out |= (1<<(num-1));
	else
		g_out &= (~(1<<(num-1)));
	switch(num)
	{
		case 1:
			if(action)
				HAL_GPIO_WritePin(OUT1_GPIO_Port,OUT1_Pin,GPIO_PIN_SET);
			else
				HAL_GPIO_WritePin(OUT1_GPIO_Port,OUT1_Pin,GPIO_PIN_RESET);
			break;
		case 2:		
			if(action)
				HAL_GPIO_WritePin(OUT2_GPIO_Port,OUT2_Pin,GPIO_PIN_SET);
			else
				HAL_GPIO_WritePin(OUT2_GPIO_Port,OUT2_Pin,GPIO_PIN_RESET);

			break;
		case 3:
			if(action)
				HAL_GPIO_WritePin(OUT3_GPIO_Port,OUT3_Pin,GPIO_PIN_SET);
			else
				HAL_GPIO_WritePin(OUT3_GPIO_Port,OUT3_Pin,GPIO_PIN_RESET);
			break;
		case 4:			
			if(action)
				HAL_GPIO_WritePin(OUT4_GPIO_Port,OUT4_Pin,GPIO_PIN_SET);
			else
				HAL_GPIO_WritePin(OUT4_GPIO_Port,OUT4_Pin,GPIO_PIN_RESET);
			break;
		case 5:			
			if(action)
				HAL_GPIO_WritePin(OUT5_GPIO_Port,OUT5_Pin,GPIO_PIN_SET);
			else
				HAL_GPIO_WritePin(OUT5_GPIO_Port,OUT5_Pin,GPIO_PIN_RESET);
			break;
	}
}
uint8_t Get_Key_State()
{
	uint8_t key_state = 0;
	key_state = HAL_GPIO_ReadPin(IN1_GPIO_Port,IN1_Pin);
	key_state |= (HAL_GPIO_ReadPin(IN2_GPIO_Port,IN2_Pin)<<1);
	key_state |= (HAL_GPIO_ReadPin(IN3_GPIO_Port,IN3_Pin)<<2);
	key_state |= (HAL_GPIO_ReadPin(IN4_GPIO_Port,IN4_Pin)<<3);
	key_state |= (HAL_GPIO_ReadPin(IN5_GPIO_Port,IN5_Pin)<<4);
	g_key = key_state;
	return key_state;
}
void  Flash_Load_Head()
{
	Flash_Read_Word(FLASH_HEAD_ADDRESS,(uint32_t*)&g_flash_head,sizeof(g_flash_head)/4);
	if(g_flash_head.total_num > 100)
	{
		g_flash_head.total_num = 0;
		for(int i=0;i<100;i++)
			g_flash_head.index[i] = 0;
	}
	SEGGER_RTT_printf(0,"total number [%d]\n",g_flash_head.total_num);
}
void  Flash_Update_Head()
{
	if(g_flash_head.total_num > 100)
	{
		g_flash_head.total_num = 100;
		Flash_Write_Word(FLASH_HEAD_ADDRESS,(uint32_t*)&g_flash_head,sizeof(g_flash_head)/4);
	}
}
void  Flash_Read_Record(uint8_t number)
{
	SEGGER_RTT_printf(0,"begin read record \n");
	Flash_Read_Word(CAL_BEGIN_ADDRESS(number),(uint32_t*)&g_flash_record,(sizeof(g_flash_record)/4));
	if(g_flash_record.pulse > 256)
	{
		SEGGER_RTT_printf(0,"err data read [%d]\n",g_flash_record.pulse);
	}
	else
	{
//		for(int i=0;i<g_flash_record.pulse;i++)
//			SEGGER_RTT_printf(0,"[%d]--[%d]\n",i,g_flash_record.count[i]);
	}
	SEGGER_RTT_printf(0,"read finish\n");
}
void  Flash_Write_Record(uint8_t number)
{
	if(g_flash_record.pulse > 250 | g_flash_record.pulse == 0) 
	{
		SEGGER_RTT_printf(0,"pulse too long\n");
		return;
	}
	Flash_Write_Word(CAL_BEGIN_ADDRESS(number),(uint32_t*)&g_flash_record,sizeof(g_flash_record)/4);
	SEGGER_RTT_printf(0,"write success\n");
	Flash_Update_Head();
}
uint32_t Flash_Get_PageAddress(uint32_t addr)
{
	if(addr % 4) return 0XFFFFFFFF;
	uint32_t page_addr = addr - (addr - 0x08000000)%2048;
	SEGGER_RTT_printf(0,"page address = [%08X]\n",page_addr);
	return page_addr;
}
void Flash_Read_Word(uint32_t addr,uint32_t* pdata,uint16_t size)
{
	if(addr % 4) return;
	for(uint32_t i=addr;i<addr+size*4;)
	{
		*pdata++ = *(__IO uint32_t*)(i);
		i += 4;
	}
}
void Flash_Write_Word(uint32_t addr,uint32_t* pdata,uint16_t size)
{
	if(addr % 4) return;
	static uint8_t tmp_buf[2048];//2kb buff to temp store flash detail.
	uint32_t page_err;
	uint32_t* p_w = (uint32_t*)tmp_buf;
	FLASH_EraseInitTypeDef erase;
	erase.NbPages = 1;
	erase.TypeErase = FLASH_TYPEERASE_PAGES;
	HAL_FLASH_Unlock();
	for(uint32_t write_size = 0;write_size<size;)
	{
		//1.get begin page addr and origin useful bytes and tmp store them.
		uint32_t begin_page_addr  = Flash_Get_PageAddress(addr + write_size);
		uint32_t origin_rest_byte = addr +write_size - begin_page_addr;
		Flash_Read_Word(begin_page_addr,(uint32_t*)tmp_buf,512);
		//2.erase this page.
		erase.PageAddress = begin_page_addr;
		HAL_FLASHEx_Erase(&erase,&page_err);
		//3.prepare new data.
		memcpy(tmp_buf+origin_rest_byte,pdata,(2048-origin_rest_byte));
		write_size += (2048-origin_rest_byte);
		//4.write data.
		for(int i=0;i<512;i++)
		{
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,begin_page_addr+i*4,*p_w);
			p_w++;
		}
	}
	HAL_FLASH_Lock();
}
void IR_Send_Code(uint8_t code_num)
{
	g_ir_state = IR_SENDING;
	memset(&g_flash_record,0,sizeof(g_flash_record));
	if(code_num > 100) return;
	Flash_Read_Record(code_num);
	if(g_flash_record.pulse > 240) return;
	//ready to send code.
	SEGGER_RTT_printf(0,"code len [%d]\n",g_flash_record.pulse);
	g_cur_send_pulse = 0;
	g_send_code_flag = 1;
	HAL_TIM_Base_Start_IT(&htim7);
}